home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Passing by reference and Returning structures
- Date: 11 Mar 1996 20:57:53 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4i244h$9ke@sparcserver.lrz-muenchen.de>
- References: <4hvc3b$avn@news2.acs.oakland.edu> <4i1nkc$ov9@hobbes.cc.uga.edu>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- greear@pollux.cs.uga.edu (Ben Greear) writes:
- >In article <4hvc3b$avn@news2.acs.oakland.edu>,
- >Monument <jggoslin@vela.acs.oakland.edu> wrote:
- >>
- >>My question is, what does the return for a structure look like. I
- >>would assume the function prototype would look something like:
- >>
- >>my_struct my_function(int matrix[])
-
- >try my_struct& my_function(int matrix[]);
- > ^ being the important part
-
- This is _not_ a valid function declaration in C. In might be a valid
- function declaration in a similar but different language.
-
- Even using a _valid_ declaration like
-
- my_struct *my_function(int matrix[]);
-
- it is debateable whether the gain in efficientcy is worth the worry
- about the validity of the pointer to the returned struct, because
- that pointer must either point to a static object, in which case
- unexpected things may happen the next time this function is called,
- or to allocated memory, in which case the calling function is
- responsible for freeing that memory exactly once. For reasonable
- small structs, plain and simple returning the "whole" struct is
- _not_ the worst approach. Even for larger structs, it is up to the
- compiler whether it will use a special calling mechanism for this
- case.
-
- >>
- >>and the call would be something like
- >>
- >>structure=my_function(matrix)
- >>
- >>where my_struct is the structure type and matrix is the variable size
- >>matrix. However, when I tried just such a setup it didn't work, gave
-
- The name "matrix" implies something that is _not_ a vector. Could this
- be related to the problem of the original poster?
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-